package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Bill;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.BillService;
import com.wf.smartcomm.server.util.IdUtils;

@Service
@Transactional
public class BillServiceImpl implements BillService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void add(Bill fee) {
		fee.setObjectId(IdUtils.uuid());
		fee.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(fee);
	}

	@Override
	public void delete(String... ids) {
		for (String id : ids) {
			factory.getCurrentSession().delete(factory.getCurrentSession().load(Bill.class, id));
		}
	}

	@Override
	public void update(Bill fee) {
		factory.getCurrentSession().merge(fee);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Bill get(String objectId) {
		return (Bill) factory.getCurrentSession().get(Bill.class, objectId);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select count(*) from Bill");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Bill order by createdAt");
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Bill get() {
		Query query = factory.getCurrentSession().createQuery("from Bill order by createdAt desc limit 1");
		return (Bill) query.uniqueResult();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Bill> listByUser(User user) {
		Query query = factory.getCurrentSession().createQuery("from Bill where user=:user and status = 0");
		query.setParameter("user", user);
		return query.list();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Bill> listByHouse(House house) {
		Query query = factory.getCurrentSession().createQuery("from Bill where house=:house and status = 0");
		query.setParameter("house", house);
		return query.list();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Bill> listByExample(House house, User user,Page page) {
		if (house != null) {
			Query query = factory.getCurrentSession().createQuery("from Bill where house=:house order by status");
			query.setParameter("house", house);
			query.setFirstResult(page.getBeginIndex());
			query.setMaxResults(page.getPageSize());
			page.setDataList(query.list());
		}
		
		if (user != null) {
			Query query = factory.getCurrentSession().createQuery("from Bill where user=:user order by status");
			query.setParameter("user", user);
			query.setFirstResult(page.getBeginIndex());
			query.setMaxResults(page.getPageSize());
			page.setDataList(query.list());
		}
		
		return null;
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Bill where user=:user order by status");
		query.setParameter("user", user);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	public void listByHouse(House house, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Bill where house=:house order by status");
		query.setParameter("house", house);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

}
